using Items;
using Services;
using UnityEngine;
using UnityEngine.Events;

public class ItemUI_Manager : MonoBehaviour
{
    public event UnityAction RightClickItem;
    public event UnityAction LeftClickItem;
    public event UnityAction AltLeftClickItem;
    public event UnityAction MouseEnterItem;

    public Player Player { get; private set; }
    public EquipmentManager EquipmentManager { get; private set; }
    private GameManager gameManager;
    public EGameState InputState => gameManager.GameState;

    public ItemStack TargetStack { get; private set; }
    public Container TargetContainer { get; private set; }
    public Container AnotherContainer { get; private set; }
    public ItemUI_OptionPanel OptionPanel { get; private set; }

    private void Awake()
    {
        OptionPanel = CommonUI.GetUIComponent<ItemUI_OptionPanel>();
        LeftClickItem += OnLeftClickItem;
        AltLeftClickItem += OnAltLeftClickItem;
    }

    private void Start()
    {
        Player = ServiceLocator.Get<ActionController>().player;
        gameManager = ServiceLocator.Get<GameManager>();
        EquipmentManager = Player.EquipmentManager;
    }

    public void InvokeMouseEvent(ItemStack target, Container container, EMouseEvent click)
    {
        TargetStack = target;
        TargetContainer = container;
        AnotherContainer = Player.GetAnotherContainer(container);
        switch (click)
        {
            case EMouseEvent.Left:
                LeftClickItem?.Invoke();
                break;
            case EMouseEvent.Right:
                if (container.GetType() != typeof(CommodityContainer))
                    RightClickItem?.Invoke();
                break;
            case EMouseEvent.AltLeft:
                AltLeftClickItem?.Invoke();
                break;
            case EMouseEvent.Enter:
                MouseEnterItem?.Invoke();
                break;
            default:
                break;
        }
    }

    private void OnLeftClickItem()
    {
        switch (InputState)
        {
            case EGameState.OpenStorage:
            case EGameState.Purchase:
                Container.TryMove(TargetContainer, AnotherContainer, TargetStack.ItemAttached, 1);
                break;
        }
    }

    private void OnAltLeftClickItem()
    {
        switch (InputState)
        {
            case EGameState.OpenStorage:
            case EGameState.Purchase:
                Container.TryMove(TargetContainer, AnotherContainer, TargetStack.ItemAttached, 5);
                break;
        }
    }
}
